<?php
/**
 * 从给定的数据表中，把数据导入库里
 * @dinglin
 */
apf_require_class('Oa_Job_Abstract');
class Oa_Job_Hr_InputCrmAccount extends Oa_Job_Abstract {

    private $read_db = "hroa_slave";//读数据库
    private $writedb = "hroa_master";//写数据库

    public function run(){
        //导入CRM帐号
        $this->_input_crm();
        //导入域帐号
        $this->_input_domain();
    }
    private function _input_crm(){
        $this->log("CRM帐号同步开始 ".date('Y-m-d H:i:s'));
        $this->_get_file('crmaccount');
        while($temp = fgetcsv($this->file)){
            $sql2 = "update user set crm_account = '".$temp[0]."' where user_code='".$temp[1]."'";
            $this->execute( $sql2,array(), $this->writedb);
            $this->_sleep();
        }
        $this->log("CRM帐号同步结束 ".date('Y-m-d H:i:s'));
    }
    private function _input_domain(){
         $this->log("域帐号同步开始 ".date('Y-m-d H:i:s'));
         $this->_get_file('domainaccount');
         while($temp = fgetcsv($this->file)){
             $sql2 = "update user set domain_account = '".$temp[1]."' where user_code='".$temp[0]."'";
             $this->execute( $sql2,array(), $this->writedb);
             $this->_sleep();
         }
        $this->log("域帐号同步结束 ".date('Y-m-d H:i:s'));
    }
    private function _get_file($name){
        $file = dirname(__FILE__).'/data/'.$name.'.csv';
        if (!file_exists($file)) {
            $this->log($file."不存在,请输入正确的问题文件");exit;
        }
        $this->file = fopen($file,"r");
    }
    private function _sleep(){
        if($this->i){
            $this->i++;
        }else{
            $this->i = 1;
        }
        if($this->i%100){}
        else{
            sleep(1);
        }
    }
}